package com.example.lee1.controller;

import com.example.lee1.entity.CategoryEntity;
import com.example.lee1.service.CategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-08-13 16:12:03
 */
@RestController
@RequestMapping("generator/category")
public class CategoryController {
  @Autowired private CategoryService categoryService;

  /** 列表 */
  @RequestMapping("/list")
  @RequiresPermissions("generator:category:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = categoryService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{id}")
  @RequiresPermissions("generator:category:info")
  public R info(@PathVariable("id") Integer id) {
    CategoryEntity category = categoryService.getById(id);

    return R.ok().put("category", category);
  }

  /** 保存 */
  @RequestMapping("/save")
  @RequiresPermissions("generator:category:save")
  public R save(@RequestBody CategoryEntity category) {
    categoryService.save(category);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  @RequiresPermissions("generator:category:update")
  public R update(@RequestBody CategoryEntity category) {
    categoryService.updateById(category);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  @RequiresPermissions("generator:category:delete")
  public R delete(@RequestBody Integer[] ids) {
    categoryService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }
}
